package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Anonymous;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.domain.entity.SysDept;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhCarInfo;
import com.xinghai.system.domain.dto.XhCarInfoDTO;
import com.xinghai.system.domain.vo.XhCarInfoVo;
import com.xinghai.system.service.ISysDeptService;
import com.xinghai.system.service.IXhCarInfoService;
import com.xinghai.system.util.BeanMapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆信息Controller
 * 
 * @author smy
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/car/info")
public class XhCarInfoController extends BaseController
{
    @Autowired
    private IXhCarInfoService xhCarInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询车辆信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(XhCarInfo xhCarInfo)
    {
        //物流公司权限
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        List<Long> deptIdList = deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        xhCarInfo.setDeptIdList(deptIdList);
        startPage();
        List<XhCarInfoDTO> list = xhCarInfoService.list(xhCarInfo);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    @Anonymous
    public TableDataInfo allList(XhCarInfo xhCarInfo)
    {
        startPage();
        List<XhCarInfoDTO> list = xhCarInfoService.allList(xhCarInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhCarInfo xhCarInfo)
    {
        List<XhCarInfoDTO> list = xhCarInfoService.list(xhCarInfo);
        ExcelUtil<XhCarInfoDTO> util = new ExcelUtil<XhCarInfoDTO>(XhCarInfoDTO.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        XhCarInfo xhCarInfo = xhCarInfoService.getInfo(id);
        XhCarInfoDTO xhCarInfoDTO = new XhCarInfoDTO();
        BeanMapperUtils.copy(xhCarInfo,xhCarInfoDTO);
        if (StringUtils.isNotEmpty(xhCarInfo.getDriverId())){
            xhCarInfoDTO.setDriverIdList(Arrays.asList(xhCarInfo.getDriverId().split(",")));
        }
        return success(xhCarInfoDTO);
    }

    /**
     * 新增车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhCarInfoVo xhCarInfoVo) throws Exception
    {
        XhCarInfo xhCarInfo = new XhCarInfo();
        BeanMapperUtils.copy(xhCarInfoVo,xhCarInfo);
        if (!xhCarInfoVo.getDriverIdList().isEmpty()) {
            xhCarInfo.setDriverId(String.join(",", xhCarInfoVo.getDriverIdList()));
        }
        return toAjax(xhCarInfoService.insert(xhCarInfo));
    }

    /**
     * 修改车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhCarInfoVo xhCarInfoVo) throws Exception {
        XhCarInfo xhCarInfo = new XhCarInfo();
        BeanMapperUtils.copy(xhCarInfoVo,xhCarInfo);
        if (!xhCarInfoVo.getDriverIdList().isEmpty()) {
            xhCarInfo.setDriverId(String.join(",", xhCarInfoVo.getDriverIdList()));
        }
        xhCarInfo.setUpdateBy(getUsername());
        return toAjax(xhCarInfoService.update(xhCarInfo));
    }

    /**
     * 删除车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id) throws Exception {
        return toAjax(xhCarInfoService.delete(id));
    }


    /**
     * 批量删除车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids) throws Exception {
        return success(xhCarInfoService.deleteBatch(ids));
    }

    /**
     * 导入车辆信息模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhCarInfo> util = new ExcelUtil<XhCarInfo>(XhCarInfo.class);
        util.importTemplateExcel(response, "车辆信息数据模板");
    }


    /**
     * 导入车辆信息
     */
    @Log(title = "车辆信息管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhCarInfo> util = new ExcelUtil<XhCarInfo>(XhCarInfo.class);
        List<XhCarInfo> xhCarInfoList = null;
        try {
            xhCarInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhCarInfoService.importData(xhCarInfoList));
    }

    /**
     * 接单页面-根据车型id查询车辆信息列表
     */
    @GetMapping("/list/by/car/type")
    public AjaxResult getCarListByCarType(XhCarInfo xhCarInfo)
    {
        return success(xhCarInfoService.getCarListByCarType(xhCarInfo));
    }
}
